<?php

namespace App\Admin\Renderable;

use App\Models\SwtGood;
use App\Models\SwtMachine;
use App\Models\SwtMachineGood;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;

class GoodsRenderable extends LazyRenderable
{
    public function grid(): Grid
    {

        $machine_id = $this->machine_id ?? '';

        if(!$machine_id){
            $source_type = 0;
        } else {
            $source_type = SwtMachine::find($machine_id)->value('source_type');
        }


        switch($source_type){
            case 1:
                $grid = $this->machine_goods($machine_id);
            break;
            case 2:
                $grid = $this->goods();
            break;
            default:
                $grid = $this->goods();
            break;
        }

        return $grid;
    }

    public function machine_goods($machine_id)
    {
        return Grid::make(SwtMachineGood::with('goods') -> where('machine_id', $machine_id), function (Grid $grid) {

            $grid->column('goods_id','ID');

            $grid->column('goods.goods_picture','图片')->image('',50,50);

            $grid->column('goods.goods_name','商品名');

            $grid->column('weight','库存数量');

            $grid->column('sell_price','售价')-> display(function ($value) {
                return bcdiv($value, 100 ,2);
            });

            $grid->column('goods.cost_price','成本价')-> display(function ($value) {
                return bcdiv($value, 1000 ,3);
            });

            $grid->quickSearch(['goods_name']);

            $grid->paginate(5);

            $grid->disableRowSelector(false);

            $grid->disableActions();
        });
    }

    public function goods()
    {
        return Grid::make(new SwtGood(), function (Grid $grid) {

            $grid->column('id');

            $grid->column('goods_picture','图片')->image('',50,50);

            $grid->column('goods_name','商品名');

            $grid->column('sell_price','售价')-> display(function ($value) {
                return bcdiv($value, 100 ,2);
            });


            $grid->column('cost_price','成本价')-> display(function ($value) {
                return bcdiv($value, 1000 ,3);
            });

            $grid->quickSearch(['goods_name']);

            $grid->paginate(5);

            $grid->disableRowSelector(false);

            $grid->disableActions();
        });
    }
}
